﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

using ArcannEngine.Scene;
using ArcannEngine.Components;
using ArcannEngine.Render;

namespace ArcannEngine.Entities
{
    public class Entity
    {
        // nazwa i tym samym identyfikator encji
        private String name;
        // Odniesienie do menadzera scena zawierajacego dana encje
        private SceneManager manager;
        // Lista uzywanych komponentow
        private Dictionary<String, Component> components;

        private Model model;
        public Model Model
        {
            get { return model; }
            set { model = value;  }
        }
        // Polozenie i rotacja obiektu
        public Vector3 position = Vector3.Zero;
        public Matrix rotation = Matrix.Identity;
        //public BoundingSphere boundingSphere = new BoundingSphere();

        public SceneManager Manager
        {
            get { return manager; }
        }

        public Entity(SceneManager sceneManager, String EntityName)
        {
            this.components = new Dictionary<String, Component>();
            this.manager = sceneManager;
            this.name = EntityName;
            //this.boundingSphere.Radius = 10;
            //this.boundingSphere.Center = this.position;
        }

        public void AddComponent(Component component)
        {
            Component checkComponent = null;
            if (components.TryGetValue(component.Name, out checkComponent))
            {
                throw new Exception("Component type " + component.Name + " already exists on this object: " + name);
            }
            components.Add(component.Name, component);
        }

        public Component GetComponent(String compName)
        {
            Component component = null;
            if (!components.TryGetValue(compName, out component))
            {
                throw new Exception("Component type " + component.Name + " doesn't exists."); 
            }
            return component;
        }

        public void RemoveComponent(Component component)
        {
            Component checkComponent = null;
            if (!components.TryGetValue(component.Name, out checkComponent))
            {
                throw new Exception("Component type " + component.Name + " doesn't exists.");
            }
            components.Remove(component.Name);
        }

        public virtual void Update(GameTime gameTime)
        {
            foreach (KeyValuePair<String, Component> pair in components)
            {
                (pair.Value).Update(gameTime);
            }
        }

        public void Draw(GameTime gameTime, List<RenderDescription> renderDescriptions)
        {
            foreach (KeyValuePair<String, Component> pair in components)
            {
                (pair.Value).Draw(gameTime, renderDescriptions);
            }
        }

        public String Name
        {
            get { return name; }
        }

    }
}
